<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 15:17
 */

namespace app\admin\controller;
use PHPExcel;
use PHPExcel_IOFactory;
use app\common\model\Site;
use app\common\model\User;
use app\common\model\Practice;
use app\common\model\Volunteer;
use app\admin\logic\PassbookLogic;
use app\admin\logic\VolunteersLogic;
use think\facade\Request;
use app\vehicle\logic\TplLogic;
class Passbook extends Base
{
    private $passbookLogic;
    private $VolunteerLogic;
    public function __construct()
    {
        parent::__construct();
        $this->VolunteerLogic = new VolunteersLogic();
        $this->passbookLogic = new PassbookLogic();
    }

    public function img(){
        $data = Request::param();
        $info = Volunteer::where('id','=',$data['id'])->find();
        $download =  new \think\response\Download('.'.$info['thumb']);
        return $download->name($data['id'].'.png');
    }

    public function index(){
        if($_SESSION['xm_travel']['admin_info']['site_id']){
            $where[] = ['site_id','=',$_SESSION['xm_travel']['admin_info']['site_id']];
        }else if($_SESSION['xm_travel']['admin_info']['oid']){
            $where[] = ['oid','=',$_SESSION['xm_travel']['admin_info']['oid']];
        }
        $volunteer = Volunteer::where($where)->select();
        $this->assign('volunteer',$volunteer);
        return $this->fetch();
    }

    public function batch(){
        $this->assign('site',Site::select());
        return $this->fetch();
    }

    public function volunteer(){
        $this->assign('site',Site::where('cate_id','eq','2')->select());
        $this->assign('site_list',Site::where('cate_id','eq','1')->select());
        return $this->fetch();
    }

    public function ajaxVolunteer(){
        $data = Request::param();
        $data['is_after'] = 1;
        $VolunteerList = $this->VolunteerLogic->VolunteerList($data, self::$nowPage);
        return json($VolunteerList);
    }
    public function exchange(){
        $where[] = ['is_after','=',1];
        if($_SESSION['xm_travel']['admin_info']['site_id']){
            $where[] = ['site_id','=',$_SESSION['xm_travel']['admin_info']['site_id']];
        }else if($_SESSION['xm_travel']['admin_info']['oid']){
            $where[] = ['oid','=',$_SESSION['xm_travel']['admin_info']['oid']];
        }
        $volunteer = Volunteer::where($where)->select();
        $this->assign('volunteer',$volunteer);
        return $this->fetch();
    }
    public function ajaxExchange(){
        $data = Request::param();
        $list = $this->passbookLogic->getExchange($data, self::$nowPage);
        return json($list);
    }

    public function add(){
        $act = 'add';
        $user = User::alias('as u')
            ->join('volunteer v','v.user_id = u.id')
            ->order('u.id desc')
            ->where('v.is_after','=',1)
            ->select();
        $this->assign('user',$user);

        $where[] = ['status','=',1];
        if($_SESSION['xm_travel']['admin_info']['site_id']){
            $where[] = ['site_id','=',$_SESSION['xm_travel']['admin_info']['site_id']];
        }else if($_SESSION['xm_travel']['admin_info']['oid']){
            $where[] = ['oid','=',$_SESSION['xm_travel']['admin_info']['oid']];
        }
        $practice = Practice::order('id desc')->where($where)->select();
        $this->assign('practice',$practice);
        $this->assign('act',$act);
        return $this->fetch();
    }

    public function ajaxIndex(){
        $where = Request::param();
        $res = $this->passbookLogic->getList($where, self::$nowPage);
        return json($res);
    }

    public function handle(){
        $data = Request::param();
        $res = $this->passbookLogic->handle($data);
        return json($res);
    }
}